import Request from '@/utils/request'

const state = () => ({
	backUrl: '/index',
})
const getters = {
	backUrl(state) {
		return state.backUrl;
	}
}
const mutations = {
	set_backUrl: (state, data) => {
		state.backUrl = data
	},
}
const actions = {
	async login({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/login/exchange',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async scanComfirm({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/greenEnergy/scanQrCodeToken',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async getExchangeInfo({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/greenEnergy/qrCodeTokenInfo',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async getExchangeItemList({commit}, params) { 
		return new Promise((resolve,reject)=>{
			const options = {
				token:params.token
			}
			delete params.token
			Request.post('/exchangeItem/list',params,{cParams:options})
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async getMyGreenEnergy({commit}, params) { 
		return new Promise((resolve,reject)=>{
			const options = {
				token:params.token
			}
			delete params.token
			Request.post('/greenEnergy/increase',params,{cParams:options})
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async createQrCodeToken({commit}, params) { 
		return new Promise((resolve,reject)=>{
			const options = {
				token:params.token
			}
			delete params.token
			Request.post('/greenEnergy/generateQrCodeToken',params,{cParams:options})
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async getQrCode({commit}, params){
		return new Promise((resolve,reject)=>{
			const options = {
				token:params.token,
			}
			delete params.token
			Request.get(`/qr/display.png?code=${params.code}`,{responseType: 'arraybuffer',cParams:options})
			.then(res=>{
				resolve(res.data) 
			})
		})
	}
}

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}
